<template>
<!-- 登录方式 -->
   <view id="loginway">
        <image class="loginimg" mode="widthFix" src="/static/logo_word.jpg"></image>
        <!-- #ifdef MP-WEIXIN -->
        <view class="lwm">
            <button class="btn1" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">微信一键登录</button>
            <!-- <span class="btn2" @click="tologin">手机号码登录/注册</span> -->
        </view>
        <!-- #endif -->
        <!-- #ifdef H5 -->
        <view class="lwm">
           <input type="text" placeholder="请输入手机号">
           <input type="text" placeholder="验证码">
           <button>登录</button>
        </view>
        <!-- #endif -->
         <p class="tips">登录代表您已同意<font>共德用户服务协议、隐私政策</font></p>
    </view>
</template>
<script>
import {login} from "@/utils/wxapi.js"
export default {
    data () {
        return {
            code: '',
            scode: ''
        }
    },
    onLoad () {
        // 清空数据
        this.$store.commit('setUserInfo',{});
        wx.removeStorage('userInfo');
        login().then(data=>{
            this.$axios({url:'api/Wechat/o2o_login',data:{code: data.code}}).then((res)=>{
                this.scode = res.data.returnData.scode;
            })
        })  
    },
    methods : {
        decryptPhoneNumber (option) {
            if(option.detail.errMsg != "getPhoneNumber:ok") {
                return false
            }
            var encryptedData = option.detail.encryptedData,iv = option.detail.iv 
            this.$axios({url:'api/Wechat/login',data:{encryptedData:encryptedData,iv:iv,scode:this.scode}}).then((res) => {
               if (res.data.Code = '200000') {
                    this.$store.commit('setUserInfo', res.data.returnData);
                    wx.setStorageSync('userInfo',res.data.returnData);
                    // 登录成功后跳到到首页
                    uni.switchTab({
                        url: '../index/index'
                    })
               } else {
                   uni.showToast({
                        title: res.data.returnMsg,
                        duration: 2000,
                        mask: true,
                        icon: 'none'
                    })
               }
            }, (error) => {
                console.log(error)
            }) 
            
        },
    },
}
</script>
<style lang="scss">
    page{background:#fff;}
    #loginway{display:flex; flex-direction: column;height:100%;
        .loginimg{display: block;width:100%;height:100%;}
        .lwm{padding: 84rpx 70rpx; flex:1;  
            .btn1{border:1px solid #096EFD;letter-spacing: 2px;border-radius:0;font-size:22rpx;padding:6rpx 0;background:#096EFD;color:#fff;}
            .btn2{display:block;text-align:center;padding-top:20rpx;color:#7A7A7A;}
        }
        .tips{padding:22rpx 0;display:block;text-align:center;font{color:#417fcc;}}
    }
</style>